const Service = require('egg').Service;

class ClassifyService extends Service {
  async lclassify() {
    // return await this.app.mysql.query('select * from classify where pid=0')
    try{
      let data = await this.app.mysql.select('classify',{
        where:{pid:0}
      })
      return {
        code:1,
        data:data
      }
    }catch(e){
      return{
        code:0,
        msg:e.message
      }
    }
  }
  /**
   * [
   *    {
   *        title:"休闲零食"，
   * list:[
   * {
   * }]
   *    }
   * ]
   */
  async rclassify(id) {
    try{
      console.log(id)
      let rArr = await this.app.mysql.select('classify',{
        where:{pid:id}
      });
      console.log(rArr)
      let data =[]
      for(let i = 0 ; i < rArr.length ; i++){
        //找出对应pid下的商品 
          let list = await this.app.mysql.select('classify',{
            where:{pid:rArr[i].id}
          })
          data.push({
              title:rArr[i].title,
              //该title下的商品列表list
              list
          })
      }
      console.log(data)
      return{
        code:1,
        data
      }
    }
    catch(e){
      return{
        code:0,
        msg:e.message
      }
    }
  }
}

module.exports = ClassifyService;